﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess
{
    /// <summary>
    /// Cashe of ObjectInfo of a type
    /// </summary>
    public class ObjectInfoCache
    {
        private static volatile object syncObj = new object();

        /// <summary>
        /// 存储类型和类型信息的字典表
        /// </summary>
        private static Dictionary<Type, ObjectInfo> cache = new Dictionary<Type, ObjectInfo>();
        private static Dictionary<Type, ObjectInfo> cacheIgnoreCase = new Dictionary<Type, ObjectInfo>();

        /// <summary>
        /// 获取对象的信息
        /// </summary>
        /// <param name="objectType">对象的类型</param>
        /// <returns></returns>
        public static ObjectInfo GetObject(Type objectType)
        {
            lock (cache)
            {
                if (cache.ContainsKey(objectType))
                {
                    return cache[objectType];
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 获取对象的信息，忽略字段大小写
        /// </summary>
        /// <param name="objectType">对象的类型</param>
        /// <returns></returns>
        public static ObjectInfo GetObjectIgnoreCase(Type objectType)
        {
            lock (cacheIgnoreCase)
            {
                if (cacheIgnoreCase.ContainsKey(objectType))
                {
                    return cacheIgnoreCase[objectType];
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 测试是否在缓存中存在指定的类型
        /// </summary>
        /// <param name="objectType">类型</param>
        /// <returns></returns>
        public static bool ContainsType(Type objectType)
        {
            return cache.ContainsKey(objectType);
        }
        /// <summary>
        /// 测试是否在缓存中存在指定的类型，忽略字段大小写
        /// </summary>
        /// <param name="objectType">类型</param>
        /// <returns></returns>
        public static bool ContainsTypeIgnoreCase(Type objectType)
        {
            return cacheIgnoreCase.ContainsKey(objectType);
        }
        /// <summary>
        /// 添加一个对象到缓存
        /// </summary>
        /// <param name="objectType">类型</param>
        /// <param name="obj">对象的信息</param>
        public static void AddObjectToCache(Type objectType, ObjectInfo obj)
        {
            lock (cache)
            {
                if (!cache.ContainsKey(objectType))
                {
                    cache.Add(objectType, obj);
                }
            }
        }
        /// <summary>
        /// 添加一个对象到缓存，忽略字段大小写
        /// </summary>
        /// <param name="objectType">类型</param>
        /// <param name="obj">对象的信息</param>
        public static void AddObjectToCacheIgnoreCase(Type objectType, ObjectInfo obj)
        {
            lock (cacheIgnoreCase)
            {
                if (!cacheIgnoreCase.ContainsKey(objectType))
                {
                    cacheIgnoreCase.Add(objectType, obj);
                }
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        public static void ClearCache()
        {
            lock (syncObj)
            {
                cache.Clear();
                cacheIgnoreCase.Clear();
            }
        }
    }
}
